<?php
namespace app\mini\controller\commons;

use app\common\model\CarBrands;
use app\common\model\CarModels;
use app\common\model\CarSeries as seriesModel;
use app\common\traits\InterfacesExceptionTrait;

class CarBrand extends Base
{

    use InterfacesExceptionTrait;

    /**
     * 品牌列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function brand()
    {
        $data['hot_brand'] = CarBrands::getHotBrand();//热门品牌
        $data['all_brand'] = CarBrands::geAllBrands();//所有品牌
        return $this->response($data);
    }


    /**
     * 品牌搜索
     * @return array
     * @throws \think\exception\DbException
     */
    public function search_brand()
    {
        $brand_name = input('name','');
        $data = CarBrands::field('id brand_id,name,letter,logo,is_hot')->where(['name'=>['like','%'.$brand_name.'%']])->select()->toArray();
        return $this->response($data);
    }

    /**
     * 车系列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function series()
    {
        $brand_id = input('brand_id',0);
        $data['list'] = seriesModel::geSeriesByBrandId($brand_id);//获取指定品牌的系列
        return $this->response($data);
    }

    /**
     * 车型列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function models()
    {
        $series_id = input('series_id',0);
        $data['list'] = CarModels::getModelsBySeriesId($series_id);//获取指定品牌的系列
        return $this->response($data);
    }

    /**
     * 车型列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function series_models()
    {
        $brand_id = input('brand_id',0);
        $series = seriesModel::geSeriesByBrandIdNew($brand_id);//获取指定品牌的系列.
        foreach ($series as &$v){
            $v['subItem'] = CarModels::getModelsBySeriesIdNew($v['id']);
        }
        return $this->response($series);
    }
}